package com.ruoyi.project.service.impl;


import com.ruoyi.data.DataUtils;
import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.project.domain.ParameterSingleVO;
import com.ruoyi.project.mapper.HouseProjectTMapper;
import com.ruoyi.project.service.IHouseProjectTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 维修项目Service业务层处理
 *
 * @author tuyeqi
 * @date 2023-05-15
 */
@Service
public class HouseProjectTServiceImpl implements IHouseProjectTService {
    @Autowired
    private HouseProjectTMapper houseProjectTMapper;

    /**
     * 查询维修项目
     *
     * @param id 维修项目主键
     * @return 维修项目
     */
    @Override
    public HouseProjectT selectHouseProjectTById(Long id) {
        return houseProjectTMapper.selectHouseProjectTById(id);
    }

    public HouseProjectT selectHouseProjectTByProjectId(String projectId) {
        return houseProjectTMapper.selectHouseProjectTByProjectId(projectId);
    }

    @Override
    public HouseProjectT selectHouseProjectTByProjectIdTemp(String projectId) {
        return houseProjectTMapper.selectHouseProjectTByProjectIdTemp(projectId);
    }


    /**
     * 查询维修项目列表
     *
     * @param houseProjectT 维修项目
     * @return 维修项目
     */
    @Override
    public List<HouseProjectT> selectHouseProjectTList(HouseProjectT houseProjectT) {

        return houseProjectTMapper.selectHouseProjectTList(houseProjectT);
    }

    @Override
    public List<HouseProjectT> selectHouseRepairTListByDate(HouseProjectT houseProjectT) {

        return houseProjectTMapper.selectHouseRepairTListByDate(houseProjectT);
    }

    @Override
    public HouseProjectT selectRepairAll(HouseProjectT houseProjectT) {
        return houseProjectTMapper.selectRepairAll(houseProjectT);
    }

    @Override
    public HouseProjectT selectHouseProjectSum(HouseProjectT houseProjectT) {

        return houseProjectTMapper.selectHouseProjectSum(houseProjectT);
    }
    /**
     * 新增维修项目
     *
     * @param houseProjectT 维修项目
     * @return 结果
     */
    @Override
    public HouseProjectT selectRepairBySignSts(HouseProjectT houseProjectT) {

        return houseProjectTMapper.selectRepairBySignSts(houseProjectT);
    }
        /**
         * 新增维修项目
         *
         * @param houseProjectT 维修项目
         * @return 结果
         */
        @Override
        @Transactional
        public int insertHouseProjectT (HouseProjectT houseProjectT)
        {

            if ("1".equals(houseProjectT.getProjectType())) {
                houseProjectTMapper.insertRepairAreaT(houseProjectT);
            } else if ("2".equals(houseProjectT.getProjectType())) {
                houseProjectTMapper.insertRepairGroupT(houseProjectT);
            } else {
                houseProjectTMapper.insertRepairUnitT(houseProjectT);
            }
            HouseProjectT repairVO= new  HouseProjectT();
            repairVO=houseProjectTMapper.getAreaInfo(houseProjectT.getAreaName());
            houseProjectT.setCommitteer(repairVO.getCommitteeName());
            houseProjectT.setCommitteeTel(repairVO.getCommitteeTel());
            houseProjectT.setCommunityer(repairVO.getCommunityName());
            houseProjectT.setCommunityTel(repairVO.getCommunityTel());
            houseProjectT.setStreeter(repairVO.getStreetName());
            houseProjectT.setStreetTel(repairVO.getStreetTel());
            houseProjectT.setHomer(repairVO.getPropertyName());
            houseProjectT.setHomeTel(repairVO.getPropertyTel());
            return houseProjectTMapper.insertHouseProjectT(houseProjectT);
        }

        /**
         * 修改维修项目
         *
         * @param houseProjectT 维修项目
         * @return 结果
         */
        @Override
        public int updateHouseProjectT (HouseProjectT houseProjectT)
        {
            return houseProjectTMapper.updateHouseProjectT(houseProjectT);
        }

        /**
         * 批量删除维修项目
         *
         * @param ids 需要删除的维修项目主键
         * @return 结果
         */
        @Override
        public int deleteHouseProjectTByIds (Long[]ids)
        {
            for (Long id : ids){
                HouseProjectT houseProjectVO=new HouseProjectT();
                houseProjectVO=houseProjectTMapper.selectHouseProjectTById(id);
                HouseProjectT repairVO=new HouseProjectT();
                repairVO.setProjectId(houseProjectVO.getProjectId());
                houseProjectTMapper.deleteRepairTByProjectId(repairVO);
            }
            return houseProjectTMapper.deleteHouseProjectTByIds(ids);
        }

        /**
         * 删除维修项目信息
         *
         * @param id 维修项目主键
         * @return 结果
         */
        @Override
        public int deleteHouseProjectTById (Long id)
        {
            return houseProjectTMapper.deleteHouseProjectTById(id);
        }
        public void deleteRepairForRefund (List<HouseProjectT> houseProjectList,HouseProjectT houseProjectVO,Long id)
        {
            for (HouseProjectT projectVO : houseProjectList) {
                /*Double subMoney= DataUtils.subMoney(DataUtils.sumMoney(projectVO.getBalanceSum(),projectVO.getPayMoney()),projectVO.getRateMoney());
                if(subMoney>0){
                    projectVO.setBalance(subMoney);
                }else{
                    projectVO.setBalance(0d);
                }*/
                

                houseProjectTMapper.updateRepairRevoke(projectVO);
            }
            houseProjectTMapper.deleteRepairForRefund(houseProjectVO);
            houseProjectTMapper.deleteHouseProjectTById(id);
        }
        public int getProjectId ()
        {
            return houseProjectTMapper.getProjectId();
        }


        public int updateSts (HouseProjectT houseProjectT,HouseProjectT houseProjectVO,List<HouseProjectT> houseProjectList)
        {
            for (HouseProjectT projectVO : houseProjectList) {
                houseProjectTMapper.updateRepairMoney(projectVO);
                houseProjectVO.setProjectId(houseProjectVO.getProjectId());
                houseProjectVO.setExecuteDate(houseProjectVO.getExecuteDate());
                houseProjectVO.setBalance(projectVO.getPayBalance());
                houseProjectVO.setHouseId(projectVO.getHouseId());
                houseProjectTMapper.updateRepairDate(houseProjectVO);
            }
            return houseProjectTMapper.updateSts(houseProjectT);
        }

    @Override
    public int updateStsTemp(HouseProjectT houseProjectT) {
        return houseProjectTMapper.updateStsTemp(houseProjectT);
    }

    public int updateRepairDate (HouseProjectT houseProjectT)
        {
            return houseProjectTMapper.updateRepairDate(houseProjectT);
        }

    @Override
    public int updateRepairMoneyTemp(HouseProjectT houseProjectT) {
        return houseProjectTMapper.updateRepairMoneyTemp(houseProjectT);
    }

    @Override
    public int updateRepairRevoke(HouseProjectT houseProjectT) {
        return houseProjectTMapper.updateRepairRevoke(houseProjectT);
    }


    public List<HouseProjectT> getProjectUser(HouseProjectT houseProjectT) {

        return houseProjectTMapper.getProjectUser(houseProjectT);
    }

    @Override
    public HouseProjectT selectBeInDebtSys() {
        return houseProjectTMapper.selectBeInDebtSys();

    }

    public List<HouseProjectT> getRepairListAll(HouseProjectT houseProjectT) {

        return houseProjectTMapper.getRepairListAll(houseProjectT);
    }
    @Override
    public ParameterSingleVO getSysParams(ParameterSingleVO vo) {
        return houseProjectTMapper.getSysParams(vo);
    }

}
